from django.contrib.auth.models import AbstractUser
from django.db import models

from dvadmin.system.models import Users
from dvadmin.utils.models import CoreModel
from ops.utils.models import table_prefix
from django.db.models import CASCADE


class Project(CoreModel):
    name = models.CharField(max_length=64, verbose_name='项目名称')
    mark = models.CharField(max_length=64, verbose_name='项目标识')
    location = models.CharField(max_length=20, verbose_name='项目归属地', default='华为云')
    project_member = models.ManyToManyField(to=Users, related_name='项目成员', db_constraint=False, blank=True)

    class Meta:
        # 自定义表名
        db_table = table_prefix + "config_project"
        verbose_name = '项目表'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return f"{self.name}"


class Env(CoreModel):
    name = models.CharField(max_length=64, verbose_name='环境名称')
    mark = models.CharField(max_length=64, verbose_name='环境标识')

    project = models.ForeignKey(to='Project', on_delete=CASCADE, verbose_name="归属项目", related_name='env_project',
                         db_constraint=False, null=True)

    class Meta:
        # 自定义表名
        db_table = table_prefix + "config_env"
        verbose_name = '环境管理'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return f"{self.name}"


class Service(CoreModel):
    name = models.CharField(max_length=64, verbose_name='服务名称')
    mark = models.CharField(max_length=64, verbose_name='服务标识')
    var = models.CharField(max_length=2048, verbose_name="自定义变量", null=True, blank=True, help_text="自定义变量")

    env = models.ForeignKey(to='Env', on_delete=CASCADE, verbose_name="所属环境", related_name='service_env',
                         db_constraint=False, null=True)

    class Meta:
        # 自定义表名
        db_table = table_prefix + "config_service"
        verbose_name = '服务管理'
        verbose_name_plural = verbose_name
        ordering = ('id',)

    def __str__(self):
        return f"{self.name}"


class Basic(CoreModel):
    name = models.CharField(max_length=64, verbose_name='配置名')
    value = models.CharField(max_length=200, verbose_name='配置存储值')
    type = models.CharField(max_length=64, verbose_name='配置类型')


    class Meta:
        # 自定义表名
        db_table = table_prefix + "config_basic"
        verbose_name = '基础配置'
        unique_together = ('type', 'name')
        verbose_name_plural = verbose_name
        ordering = ('id',)

